<template>
    <div class="monitor-database">
        <div style="height: 35px">
            <el-form :model="searchForm" :inline="true" size="mini">
                <el-form-item label="name">
                    <el-input v-model="searchForm.name"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="listDatabase">Search</el-button>
                    <el-button type="primary" @click="listDatabase">Refresh</el-button>
                    <el-button type="primary" @click="handleCreate">New</el-button>
                </el-form-item>
            </el-form>
        </div>
        <el-table
                v-infiniteScroll="infiniteScroll"
                border
                default-expand-all
                highlight-current-row
                :data="databaseTable"
                row-key="ukId"
                height="500"
                size="mini"
                style="width: 100%"
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
            <el-table-column
                    prop="name"
                    label="name"
                    width="*">
            </el-table-column>
            <el-table-column
                    prop="department"
                    label="department"
                    width="150">
            </el-table-column>
            <el-table-column
                    prop="project"
                    label="project"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="responsible"
                    label="responsible"
                    width="200">
            </el-table-column>
            <el-table-column
                    prop="node"
                    label="node"
                    width="120">
            </el-table-column>
            <el-table-column
                    show-overflow-tooltip
                    prop="ip"
                    label="ip"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="status"
                    label="status"
                    width="70">
                <template slot-scope="scope">
                    <el-switch
                            @change="statusChange($event, scope.row.ukId)"
                            :value="scope.row.status != '1'"
                            active-color="#909399"
                            inactive-color="#67c23a">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column
                    prop="sort"
                    label="sort"
                    width="60">
            </el-table-column>
            <el-table-column
                    label="操作"
                    width="215">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="handleDetails(scope.row.grafana)">详情
                    </el-button>
                    <el-button
                            size="mini"
                            @click="handleEdit(scope.row)">编辑
                    </el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.row.ukId)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog title="数据库管理" :visible.sync="showDatabaseForm" :close-on-click-modal="false" label-width="80px">
            <el-form ref="form" :model="databaseForm" label-width="120px">
                <el-form-item label="数据库名称">
                    <el-input v-model="databaseForm.name"></el-input>
                </el-form-item>
                <el-form-item label="部门">
                    <el-input v-model="databaseForm.department"></el-input>
                </el-form-item>
                <el-form-item label="项目">
                    <el-input v-model="databaseForm.project"></el-input>
                </el-form-item>
                <el-form-item label="节点">
                    <el-input v-model="databaseForm.node"></el-input>
                </el-form-item>
                <el-form-item label="Ip">
                    <el-input v-model="databaseForm.ip"></el-input>
                </el-form-item>
                <el-form-item label="grafana地址">
                    <el-input v-model="databaseForm.grafana"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-select v-model="databaseForm.status" placeholder="请选择状态">
                        <el-option
                                v-for="item in this.$store.state.Pabu.dict.manage.STATUS.dictItems"
                                :key="item.ukId"
                                :label="item.name"
                                :value="parseInt(item.value)">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="排序">
                    <el-input v-model="databaseForm.sort" type="number" min="0" max="90000"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleSave">保存</el-button>
                    <el-button @click="showDatabaseForm = false">取 消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
        <el-dialog fullscreen title="数据库详情" :visible.sync="showDatabaseGrafana" :close-on-click-modal="false" >
            <iframe :src="databaseGrafana" width="100%" height="830px" frameborder="0"></iframe>
        </el-dialog>
    </div>
</template>

<script>
  import {deleteDatabase, listDatabase, saveDatabase, updateDatabaseStatus} from "../../../api/monitor/database";
  import PageUtils from "../../../utils/PageUtils";

  export default {
    name: "Database",
    data () {
      return {
        showDatabaseForm: false,
        showDatabaseGrafana: false,
        databaseGrafana: "",
        databaseTable: [],
        searchForm: {},
        databaseForm: {},
        pagination: {
          total: 0,
          size: 30,
          current: 1,
        }
      }
    },
    methods: {
      handleCreate () {
        this.databaseForm = {
          status: 1
        }
        this.showDatabaseForm = true
      },
      handleEdit (row) {
        this.databaseForm = row
        this.showDatabaseForm = true
      },
      handleSave () {
        saveDatabase(this.databaseForm).then(result => {
          this.listDatabase()
          this.showDatabaseForm = false
        }).catch(error => {
          console.log(error)
        })
      },
      handleDelete (ukId) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteDatabase(ukId).then(result => {
            this.listDatabase()
            this.showDatabaseForm = false
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }).catch(error => {
            console.log(error)
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      handleDetails(grafana) {
        this.showDatabaseGrafana = true
        this.databaseGrafana = grafana
      },
      statusChange (value, paginationId) {
        let status = value ? 0 : 1
        updateDatabaseStatus(paginationId, status).then(result => {
          this.listDatabase()
        }).catch(error => {
          console.log(error)
        })
      },
      infiniteScroll () {
        let current = this.$store.state.Pabu.pagination.database.current + 1
        let size = this.$store.state.Pabu.pagination.database.size
        listDatabase(this.searchForm, current, size).then(result => {
          if (result.data.databases.records.length !== 0) {
            for (let database of result.data.databases.records) {
              this.databaseTable.push(database)
            }
            PageUtils.setPagination(this, "database", result.data.databases)
          }
        }).catch(error => {
          console.log(error)
        })
      },
      listDatabase () {
        listDatabase(this.searchForm, 1, 30).then(result => {
          this.databaseTable = result.data.databases.records
          PageUtils.setPagination(this, "database", result.data.databases)
        }).catch(error => {
          console.log(error)
        })
      }
    },
    created () {
      this.listDatabase()
    }
  }
</script>

<style scoped>
    .monitor-database {
        width: 100%;
        height: 100%;
    }

    /* element ui样式重写 */
    .monitor-database > .el-table {
        height: calc(100% - 35px) !important;
    }

    .monitor-database > .el-table > .el-table__body-wrapper {
        height: calc(100% - 35px) !important;
    }
</style>